/*---------------------注册全局过滤器----------------------*/
//无数据情况处理，传入空字符串
const judgeIsNull = function (val) {
  const target = typeof val === 'object' ? Object.prototype.toString.call(val) : `${val}`;
  if (/null|undefined|nan|^\s*$/i.test(target)) {
    return '--';
  }
  return val;
};

//保留两位小数处理
const decimal = function (val) {
  return judgeIsNull(val) === '--' ? '--' : parseFloat(val).toFixed(2);
};

//针对正数加上+
const positive = function (val) {
  if (judgeIsNull(val) === '--') {
    return '--';
  } else if (parseFloat(val) > 0) {
    return `+${val}`;
  } else {
    return val;
  }
};

//针对有值的带上百分号
const addPercentSymbol = function (val) {
  return judgeIsNull(val) === '--' ? '--' : `${val}%`;
};

//去除股票名称的空格
const removeSpace = function (val) {
  return judgeIsNull(val) === '--' ? val.replace(/\s/g, '') : '--';
};

//时间戳转换为日期
const stampToDate = function (val) {
  return Boolean(+val) ? new Date(+val).toTimeString().split(' ')[0] : '--';
};

//自定义单位
const defindedSign = function (val, sign) {
  if (judgeIsNull(val) === '--') {
    return '--';
  } else {
    return sign ? `${val}${sign}` : val;
  }
};
/*---------------------------------   指令   ------------------------------*/
//指令
const fontresizehandler = function (el, binding) {
  el.classList.remove('ellipsis');
  let originSize = el.getAttribute('origin-fontsize');
  if (!originSize) {
    originSize = parseFloat(getComputedStyle(el, '')['font-size']);
    el.setAttribute('origin-fontsize', String(originSize));
  }
  el.style.fontSize = `${originSize}px`;

  if (!originSize) return;
  if (el.scrollWidth - el.offsetWidth <= 0) {
    el.style.fontSize = `${originSize}px`;
    return;
  }
  const dpr = document.documentElement.getAttribute('data-dpr');
  const overflowrate = Number(
    Number(1 + (el.scrollWidth - el.offsetWidth) / el.offsetWidth).toFixed(2)
  );
  const fontsize = parseFloat(getComputedStyle(el, '')['font-size']);
  const minSize = dpr * ((binding.value && binding.value.minSize) || 12);

  let newSize;
  newSize = fontsize / overflowrate;
  if (newSize < minSize) {
    newSize = minSize;
    el.classList.add('ellipsis');
  }
  el.style.fontSize = `${newSize}px`;
};

// eslint-disable-next-line complexity
const redgreenhandler = function (el, binding) {
  const currentNumber = parseFloat(el.innerText);
  const referenceValue = (binding.value && binding.value.referenceValue) || null;
  const noRedGreen = (binding.value && binding.value.noRedGreen) || false;
  if (judgeIsNull(el.innerText) === '--' || noRedGreen) {
    return;
  }
  if (referenceValue && referenceValue > 0) {
    el.classList.add('hxm-red');
  } else if (referenceValue && referenceValue < 0) {
    el.classList.add('hxm-green');
  } else {
    currentNumber > 0 ? el.classList.add('hxm-red') : el.classList.add('hxm-green');
  }
};
const redgreenhandlerUpdate = function (el, binding) {
  if (!binding.oldValue) {
    return;
  }
  el.classList.remove('hxm-red');
  el.classList.remove('hxm-green');
  redgreenhandler(el, binding);
};

const detectionEmpty = function (el) {
  el.innerText = judgeIsNull(el.innerText);
};

const detectionEmptyUpdate = function (el, binding) {
  if (!binding.oldValue) {
    return;
  }
  el.innerText = judgeIsNull(el.innerText);
};

module.exports = {
  filterExt: {
    judgeIsNull,
    decimal,
    positive,
    addPercentSymbol,
    removeSpace,
    stampToDate,
    defindedSign,
  },
  directivesExt: {
    fontresizehandler,
    redgreenhandler,
    redgreenhandlerUpdate,
    detectionEmpty,
    detectionEmptyUpdate,
  },
};
